package com.glgc.projecti.web.controller.client;

import java.util.Iterator;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import lombok.ToString;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.glgc.projecti.common.BizException;
import com.glgc.projecti.entity.dto.SignDto;
import com.glgc.projecti.service.IHttpService;
import com.glgc.projecti.web.controller.base.BaseController;

@RequestMapping("/client")
@Controller
public class ClientLoginController extends BaseController {

	@Resource
	IHttpService httpService;
	
	@RequestMapping("/register.do")
	public @ResponseBody JSONObject register(SignDto dto, HttpSession session) {
		try {
			httpService.signup(dto);
			return this.toJSONResult(true);
		} catch (BizException e) {
			return this.toJSONResult(false, e.getErrCode());
		}
	}
	
	private JSONObject getDataset(JSONObject result) {
		try {
			JSONObject tempJSON = result.getJSONObject("result")
					.getJSONArray("dataset0").getJSONObject(0);
			return tempJSON;
		} catch (Exception e) {
			return null;
		} 
	}

	private JSONArray getDatasetArray(JSONObject result) {
		JSONArray tempJSON = null;
		try {
			tempJSON = result.getJSONArray("schools");
		} catch (Exception e) {
			// return null;
		}

		return tempJSON;
	}
	
	private JSONArray getRankDatasetArray(JSONObject result) {
		JSONArray tempJSON = null;
		try {
			tempJSON = result.getJSONArray("ranks");
		} catch (Exception e) {
			// return null;
		}

		return tempJSON;
	}

	@RequestMapping("/sendValiCode.do")
	public @ResponseBody JSONObject sendValiCode(String mobileTel,
			HttpSession session) {
		// mobileTel = "18626095819";
		
		try {
			httpService.sendValiCode(mobileTel);
		} catch (BizException e) {
			String error_code = e.getErrCode();
			return this.toJSONResult(false, error_code);
		}
		return this.toJSONResult(true);
	}
	
	@RequestMapping("/getSchoolList.do")
	public @ResponseBody JSONObject getSchoolList(HttpSession session) {
		JSONObject result;
		try {
			result = httpService.getSchoolList();
			if (result != null) {
				JSONArray resultJson = getDatasetArray(result);
				if (resultJson == null) {
					return this.toJSONResult(false);
				}
				return this.toJSONResult(true, resultJson);
			} else {
				return this.toJSONResult(false);
			}
		} catch (BizException e) {
			String error_code = e.getErrCode();
			return this.toJSONResult(false, error_code);
		}
	}

	@RequestMapping("/getRankList.do")
	public @ResponseBody JSONObject getRank(HttpSession session) {
		JSONObject result;
		try {
			result = httpService.getRankList();
			if (result != null) {
				JSONArray resultJson = getRankDatasetArray(result);
				if (resultJson == null) {
					return this.toJSONResult(false);
				}
				//过滤姓名和手机号码
				Iterator<JSONObject> iterator = resultJson.iterator();
				while (iterator.hasNext()) {
					JSONObject resMap = iterator.next();
					resMap.put("cellphone", subTel(resMap.getJSONObject("user").getString("cellphone")));
					resMap.put("school", resMap.getJSONObject("user").getJSONObject("school").getString("name"));
					resMap.remove("match");
					resMap.remove("user");
				}
				return this.toJSONResult(true, resultJson);
			} else {
				return this.toJSONResult(false);
			}
		} catch (BizException e) {
			String error_code = e.getErrCode();
			return this.toJSONResult(false, error_code);
		}
	}

	private static  String subTel(String tel) {
		String tel1 = tel.substring(0, 3);
		String tel2 = tel.substring(tel.length() - 4, tel.length());
		return tel1 + "****" + tel2;
	}
}
